package team.se.tms.dto;

import com.google.gson.Gson;
import lombok.Data;
import team.se.tms.enums.DateEnum;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;


/**
 * 用于分页的工具类
 *
 * @author johnny
 * @date 2018/11/23
 */
@Data
public class Page<T> {
    //开始位置
    private int start = 0;
    //每页多少条数据,默认10条，如果dataTable修改默认，这里需要修改
    private int length = 10;
    //排序的字段值，默认按第一列排序
    private String sortField;
    //排序的方向，desc or asc
    private String orderDir;
    //总数据量
    private int totalRecord=0;
    //查询结果
    private List<T> list;
    //搜索值
    private String searchValue;
    //连接的表字段
    private String concatTableField;
    //重绘
    private String draw;

    /**
     * 设置查询结果
     *
     * @param list 查询结果
     * @create on 2018/11/23 by johnny
     */
    public void setRecord(List<T> list){
        this.list = list;
    }
    /**
     * 获取封装好的dataTaeble数据
     *
     * @return String json数据
     * @create on 2018/11/23 by johnny
     */
    public String getDataTabelJson(List<T> tList){
        Gson gson = new Gson();
        String json =
                "{" +
                        "\"draw\": "+this.getDraw()+","
                    +   "\"recordsTotal\": " +this.getTotalRecord() + ","
                    +   "\"recordsFiltered\": "+this.getTotalRecord()+ ","
                    +   "\"data\": " +gson.toJson(tList) +","
                    +   "\"success\": true "
                +"}";
        return json;
    }
    public String getDataTabelJson(List<T> tList, DateEnum dateEnum){
        Gson gson = new Gson();
        String json =
                "{" +
                        "\"draw\": "+this.getDraw()+","
                        +   "\"recordsTotal\": " +this.getTotalRecord() + ","
                        +   "\"recordsFiltered\": "+this.getTotalRecord()+ ","
                        +   "\"data\": " +JSON.toJSONStringWithDateFormat(tList,dateEnum.getFormat()) +","
                        +   "\"success\": true "
                        +"}";
        return json;
    }
    /**
     * 方法描述
     *
     * @return String 返回查询字段，若为空则返回 1
     * @create on 2018/11/24 by johnny
     */
    public String getSortField() {
        if(sortField!=null && !"".equals(sortField)){
            return changToDownLine(sortField);
        }
        return "1";
    }
    /**
     * 方法描述
     *
     * @return String 返回排序方向，若为空则返回 asc
     * @create on 2018/11/24 by johnny
     */
    public String getOrderDir() {
        if(orderDir!=null){
            return orderDir;
        }
        return "asc";
    }
    public String getConcatTableField() {
        return changToDownLine(concatTableField);
    }

    /**
     * 驼峰命名转下划线
     *
     * @param param 驼峰命名
     * @return String 下划线命名
     * @create on 2018/11/24 by johnny
     */
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    public String changToDownLine(String sortField){
        if(sortField==null){
            return "";
        }
        Matcher matcher = humpPattern.matcher(sortField);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
